package com.junzhiit.nengji.cms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.WechatConfigBO;
import com.junzhiit.nengji.cms.service.WechatConfigService;
import com.junzhiit.nengji.cms.web.exception.ShopIsExistException;
import com.junzhiit.nengji.cms.web.exception.WechatConfigAppIdExistException;
import com.junzhiit.nengji.cms.web.exception.WechatConfigNotFoundException;
import com.junzhiit.nengji.cms.web.exception.WechatConfigSecretExistException;
import com.junzhiit.nengji.cms.web.util.OrderNumberUtils;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.util.tools.NumberUtils;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.WechatConfigAddVO;
import com.junzhiit.nengji.cms.web.vo.WechatConfigEditVO;
import com.junzhiit.nengji.cms.web.vo.WechatConfigVO;
import com.junzhiit.nengji.dao.WechatConfigDAO;
import com.junzhiit.nengji.dao.cache.WechatConfigCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.WechatConfig;

@Service
public class WechatConfigServiceImpl extends BaseServiceImpl implements WechatConfigService {

	@Autowired
	private WechatConfigDAO wechatConfigDAO;
	
	@Autowired
	private WechatConfigCacheDAO wechatConfigCacheDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, WechatConfigVO wechatConfigVO) {
		WechatConfigBO wechatConfigBO = new WechatConfigBO();
		BeanUtils.copyProperties(wechatConfigVO, wechatConfigBO);
		List<WechatConfigBO> list = wechatConfigDAO.findAll(dtp.getStart(), dtp.getLength(), wechatConfigBO);
		int totalCount = wechatConfigDAO.getTotalCount(wechatConfigBO);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO add(WechatConfigAddVO wechatConfigAddVO) {
		ResultVO result = new ResultVO();
		WechatConfig wechatConfig = new WechatConfig();
		BeanUtils.copyProperties(wechatConfigAddVO, wechatConfig);
		int count = wechatConfigDAO.checkShopId(wechatConfigAddVO.getCsId());
		if (count > 0) {
			throw new ShopIsExistException("已存在");
		}
		if (wechatConfigDAO.getAppIdExist(wechatConfig.getAppId()) > 0) {
			throw new WechatConfigAppIdExistException("微信配置appId已经存在");
		}
		if (wechatConfigDAO.getSecretExist(wechatConfig.getSecret()) > 0) {
			throw new WechatConfigSecretExistException("微信配置secret已经存在");
		}
		List<String > wcId = NumberUtils.getStringRandom(1, 16);
		wechatConfig.setWcId(OrderNumberUtils.WECHAT_CONFIG_NUMBER+wcId.get(0).toLowerCase());
		WechatConfig params = wechatConfigDAO.insert(wechatConfig);
		String key = DIYKey.getWechatCodeKey(params.getWcId());
		wechatConfigCacheDAO.addHash(key, params);
		result.setData(params);
		return result;
	}

	@Override
	public ResultVO delete(String id) {
		ResultVO result = new ResultVO();
		try {
			result.setData(wechatConfigDAO.updateToDelete(id));
		} catch (Exception e) {
			throw new WechatConfigNotFoundException("该微信配置不存在！");
		}
		return result;
	}

	@Override
	public ResultVO edit(WechatConfigEditVO wechatConfigEditVO) {
		WechatConfig wechatConfig = new WechatConfig();
		BeanUtils.copyProperties(wechatConfigEditVO, wechatConfig);
		ResultVO result = new ResultVO();
		WechatConfig param = wechatConfigDAO.query(wechatConfigEditVO.getId());
		if (param == null) {
			throw new WechatConfigNotFoundException("该微信配置不存在！");
		}
		/*if (!param.getCsId().equals(wechatConfigEditVO.getShopId())) {
			int count = wechatConfigDAO.checkShopId(wechatConfigEditVO.getShopId());
			if (count > 0) {
				throw new ShopIsExistException("该店铺已存在");
			}
		}*/
	/*	if (!param.getAppId().equals(wechatConfig.getAppId())) {
			if (wechatConfigDAO.getAppIdExist(wechatConfig.getAppId()) > 0) {
				throw new WechatConfigAppIdExistException("微信配置appId已经存在");
			}
		}
		if (!param.getSecret().equals(wechatConfig.getSecret())) {
			if (wechatConfigDAO.getSecretExist(wechatConfig.getSecret()) > 0) {
				throw new WechatConfigSecretExistException("微信配置secret已经存在");
			}
		}*/
		/*List<String > wcId = NumberUtils.getStringRandom(1, 16);
		wechatConfig.setWcId(OrderNumberUtils.WECHAT_CONFIG_NUMBER+wcId.get(0).toLowerCase());*/
		//BeanUtils.copyProperties(wechatConfig,param);
		
		param.setUpdatedTime(new Date());
		param.setPaySecret(wechatConfigEditVO.getPaySecret());
		boolean isUpdate = wechatConfigDAO.update(param);
		if(isUpdate){
			String key = DIYKey.getWechatCodeKey(param.getWcId());
			wechatConfigCacheDAO.addHash(key, param);
		}
		return result;
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO result = new ResultVO();
		WechatConfigBO param = wechatConfigDAO.getWechatConfigById(id);
		if (param == null) {
			throw new WechatConfigNotFoundException("该微信配置不存在！");
		}
		result.setData(param);
		return result;
	}

}
